package see.fa.texttransformer;

import javax.script.Bindings;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;

public class TextTransformer {

    public String execute(String sourceText,
            TransformDetails transformDetails) throws TextTransformationException {
        MutableSource mutableSource = new MutableSource(sourceText);
        Bindings bindings = new SimpleBindings();
        bindings.put("source", mutableSource);
        try {
            new ScriptEngineManager().getEngineByName(transformDetails.getSource()).eval(transformDetails.getTransformationScript(), bindings);
        } catch (ScriptException e) {
            throw new TextTransformationException("Error occurred while executing transformation script.", e);
        }
        return mutableSource.get();
    }

}
